﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;

namespace FTFSmartCar
{
    public partial class RPMViewerForm : Form
    {

        #region visualization variables
        PointPairList RPMPointPairList = new PointPairList();
        GraphPane MyPane = new GraphPane();

        public const int SampleRate = 10;
        public const int TimeHistoryToKeep = 5;
        public const int RPMHistoryLength = TimeHistoryToKeep * SampleRate;

        double[] RPMHistory = new double[RPMHistoryLength];
        int RPMHistoryIndex;

        #endregion

        public RPMViewerForm()
        {
            InitializeComponent();
            InitRPMViewer();
            ResizeGraph();
        }

        private void RPMViewerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

          public void PostNextRPMReading(double RPMIn)
        {
            RPMHistory[RPMHistoryIndex] = RPMIn;
            RPMHistoryIndex++;
            if (RPMHistoryIndex >= RPMHistoryLength)
                RPMHistoryIndex= 0;
        }

        void InitRPMViewer()
        {
              
                MyPane.Title.Text = "RPM";
                MyPane.XAxis.Title.Text = "Time (Seconds)";
                MyPane.YAxis.Title.Text = "RPM";
                MyPane.YAxis.Scale.MaxAuto = false;
                MyPane.YAxis.Scale.MinAuto = false;
                MyPane.YAxis.Scale.Min = 0;
                MyPane.YAxis.Scale.Max = 5000;
                MyPane.YAxis.MajorGrid.IsVisible = true;

                MyPane.XAxis.Scale.MaxAuto = false;
                MyPane.XAxis.Scale.MinAuto = false;
                MyPane.XAxis.Scale.Min = -TimeHistoryToKeep;
                MyPane.XAxis.Scale.MinAuto = false;
                MyPane.XAxis.Scale.Max = 0;

             

                MyPane.XAxis.MajorGrid.IsVisible = true;

                RPMGraph.MasterPane.PaneList.Clear();
                RPMGraph.MasterPane.PaneList.Add(MyPane);
                
            RPMGraph.AxisChange();

          
            for(int i=0;i<RPMHistoryLength;i++)
            {
                RPMHistory[i] = 0;
            }

        }
        private void UpdateGraph()
        {
            double t = 0;

           RPMPointPairList.Clear();

           int CurrentIndex = RPMHistoryIndex;

           for (int i = 0; i < RPMHistoryLength; i++)
                {


                    t = (((double)i / (double)RPMHistoryLength) * (double)TimeHistoryToKeep) - (double)TimeHistoryToKeep;

                        RPMPointPairList.Add(t, RPMHistory[CurrentIndex]);

                        CurrentIndex++;

                        if (CurrentIndex >= RPMHistoryLength)
                            CurrentIndex = 0;
                 }


           MyPane.CurveList.Clear();
           MyPane.AddCurve("RPM", RPMPointPairList, Color.Red, SymbolType.Circle);
           RPMGraph.AxisChange();
           RPMGraph.Invalidate();
         }
        private void RPMViewerForm_Resize(object sender, EventArgs e)
        {
            ResizeGraph();
        }

        void ResizeGraph()
        {
            RPMGraph.Location = new Point(10, 10);
            // Leave a small margin around the outside of the control
            RPMGraph.Size = new Size(ClientRectangle.Width - 20,
                                    ClientRectangle.Height - 20);

            UpdateGraph();
        }

        private void WindowUpdateTimer_Tick(object sender, EventArgs e)
        {
            UpdateGraph();
            this.Invalidate();
        }
    }
}
